package distributedlock.zk;

import distributedlock.zk.ConcurrentTest.ConcurrentTask;

public class ZkTest {

	private static String config = "192.168.6.41:7181";
	private static String config2 = "192.168.6.41:7182";
	private static String lockName = "amsName";
	private static String lockName2 = "amsName2";

	public static void main(String[] args) {
		Runnable task1 = new Runnable() {
			public void run() {
				DistributedLock lock = null;
				try {
					lock = new DistributedLock(config, lockName);
					lock.lock();
					Thread.sleep(3000);
					System.out.println("===Thread " + Thread.currentThread().getId() + " running");
				} catch (Exception e) {
					e.printStackTrace();
				} finally {
					if (lock != null)
						lock.unlock();
				}

			}

		};
		new Thread(task1).start();
		
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e1) {
			e1.printStackTrace();
		}
		
		ConcurrentTask[] tasks = new ConcurrentTask[60];
		for (int i = 0; i < tasks.length; i++) {
			ConcurrentTask task2 = new ConcurrentTask() {
				public void run() {
					DistributedLock lock = null;
					try {
						lock = new DistributedLock(config2, lockName2);
						lock.lock();
						System.out.println("Thread " + Thread.currentThread().getId() + " running");
					} catch (Exception e) {
						e.printStackTrace();
					} finally {
						lock.unlock();
					}

				}
			};
			tasks[i] = task2;
		}
		new ConcurrentTest(tasks);
	}
}